/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.cd.impl;

import java.awt.Color;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.ImageIcon;

import org.jdom.Element;

import who.tmap.bo.BizObject;
import who.tmap.bo.cd.CDPointSymbols;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.point.CDPointDataDrivenEntry;
import who.tmap.bo.cd.point.CDPointDataDrivenSymbol;
import who.tmap.common.TMapException;
import who.tmap.dao.TMapDAOException;

public class CDPointDataDrivenSymbolDAOImpl extends BaseCartographicDisplayDAOImpl {

	CDPointDataDrivenSymbolDAOImpl() {
		super();
	}
	
	@SuppressWarnings("unchecked")
	public BizObject loadXml(Element elt) throws TMapDAOException {
		CDPointDataDrivenSymbol cd =null;
	
		try {
			cd =(CDPointDataDrivenSymbol)getCD( elt );
			Element mainElt =elt.getChild( "PointDataDrivenSymbol" );
			
			cd.setJoinIdColumnName( "" );
			cd.setDataColumnName( "" );
			
			cd.setXColumn( getNullStringIfEmpty( mainElt.getAttributeValue( "xColumnName" ) ) );
			cd.setYColumn( getNullStringIfEmpty( mainElt.getAttributeValue( "yColumnName" ) ) );
			cd.setDescriptorColumn( mainElt.getAttributeValue( "descriptorColumnName" ) );		
			cd.setScalingFactor( mainElt.getAttribute( "scalingFactor" ).getIntValue() );	
			cd.setDataColumn( mainElt.getAttributeValue( "dataColumnName" ) );
			
			cd.setCDPointType( CDTypes.POINT_DATA_DRIVEN_SYMBOL );
			
			Map<String, CDPointDataDrivenEntry> entries =new TreeMap<String, CDPointDataDrivenEntry>( String.CASE_INSENSITIVE_ORDER );
			cd.setEntries( entries );
			
			List entriesElt =mainElt.getChild( "Entries" ).getChildren();
			Iterator it =entriesElt.listIterator();
			while( it.hasNext() ) {
				Element entryElt =(Element)it.next();
				
				CDPointDataDrivenEntry entry =(CDPointDataDrivenEntry)cdf.get( CDTypes.POINT_DATA_DRIVEN_ENTRY );
				
				entry.setVal( entryElt.getAttributeValue( "value" ) );
				
				int symbolId =-1;
				String symbolName =entryElt.getAttributeValue( "shape" );
				
				for( int ct =0; ct < CDPointSymbols.names.length; ct++ ) {
					if ( CDPointSymbols.names[ ct ].equalsIgnoreCase( symbolName ) ) {
						symbolId =ct;
						break;
					}
				}		
				
				if ( symbolId == -1 ) {
					throw new TMapException( "Invalid shape name [" + symbolName + "]" );
				}
				
				entry.setSymbol( symbolId );
				if ( symbolId == CDPointSymbols.BITMAP ) {
		    		byte[] imageData =miscHelper.loadInlineImage( entryElt.getChild( "InlineBitmap" ) );
		    		
		    		entry.setImageBytes( imageData );
		        	ImageIcon imageIcon =new ImageIcon( imageData );
		        	entry.setImage( imageIcon.getImage() );
				}				
				
				entry.setFillPattern( entryElt.getAttribute( "fillPattern" ).getIntValue() );			
				entry.setColor( miscHelper.rgbToColor( entryElt.getAttributeValue( "color" ) ) );
				entry.setSize( entryElt.getAttribute( "size" ).getIntValue() );
				
				entries.put( entry.getVal(), entry );
			}
			
			
		} catch( Exception e ) {
			throw new TMapDAOException( e );
		}
		
		
		return cd;
	}

	public Element saveXml(BizObject object) throws TMapDAOException {
		CDPointDataDrivenSymbol cd =(CDPointDataDrivenSymbol)object;
		
        Element rootElt =super.saveXml( object );
        Element mainElt =new Element( "PointDataDrivenSymbol" );
        rootElt.addContent( mainElt );
        
        mainElt.setAttribute( "version", VERSION );
        
        mainElt.setAttribute( "xColumnName", cd.getXColumn() ); 
        mainElt.setAttribute( "yColumnName", cd.getYColumn() );  
        mainElt.setAttribute( "descriptorColumnName", cd.getDescriptorColumn() );
        mainElt.setAttribute( "scalingFactor", Integer.toString( cd.getScalingFactor() ) ); 
        mainElt.setAttribute( "dataColumnName", cd.getDataColumn() );        
        
        Element entriesElt =new Element( "Entries" );
        mainElt.addContent( entriesElt );
        
        for( CDPointDataDrivenEntry entry : cd.getEntries().values() ) {
        	Element entryElt =new Element( "Entry" );
        	entriesElt.addContent( entryElt );
        	
        	entryElt.setAttribute( "value", entry.getVal() );
        	entryElt.setAttribute( "shape", CDPointSymbols.names[ entry.getSymbol() ] );
        	entryElt.setAttribute( "color", miscHelper.colorToHexa( entry.getColor() != null 
																					   ? entry.getColor() 
																					   : Color.BLACK ) ); 
        	entryElt.setAttribute( "fillPattern", Integer.toString( entry.getFillPattern() ) ); 
        	entryElt.setAttribute( "size", Integer.toString( entry.getSize() ) );
        	
    		if ( entry.getSymbol() == CDPointSymbols.BITMAP ) {
    	        Element inlineElt =new Element( "InlineBitmap" );
    	        entryElt.addContent( inlineElt );
    	        inlineElt.setText( miscHelper.saveInlineImage( entry.getImageBytes() ) );
    		}
        }
        
		return rootElt;
	}

}
